<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 13:01
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\View;
use think\facade\Filesystem;

class employee extends AdminBase
{
    public function index()
    {
        $agency = \app\common\model\Agency::getAgency();
        $count = Db::name('employee')->count();
        return View::fetch('index',['agency'=>$agency,'ecount'=>$count]);
    }

    //分类管理
    public function employee_json()
    {

        $start_time = input('start_time',strtotime('1970-10-01'));
        $end_time = input('end_time',time());
        $ccode = input('ccode');
        $code = input('code');
        $aid = input('aid');

        if(input('start_time') != ''){
            $start_time = strtotime($start_time);
        }else{
            $start_time = strtotime('1970-10-01');
        }

        if(input('end_time') != ''){
            $end_time = strtotime($end_time);
        }else{
            $end_time = time();
        }

        $map = [['a.create_time','between',[$start_time,$end_time]]];
        if(!empty($aid)){
            array_push($map,['a.aid','=',$aid]);
        }

        if(!empty( $ccode)){
            array_push($map,['b.code','like','%'.$ccode.'%']);
        }

        if(!empty($code)){
            array_push($map,['a.code','like','%'.$code.'%']);
        }

        $list = Db::name('employee')
            ->alias('a')
            ->join('channel b','a.channelid = b.id','left')
            ->join('groups c','a.gid = c.id','left')
            ->where($map)
            ->order('a.create_time')
            ->field('a.*,b.name as cname,b.code as ccode,c.name as groupsname')->paginate(10)->each(function($item, $key){
            $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
            return $item;
        });

        $this->result($list);
    }

    public function addgroup(){
        if ($this->request->isPost()) {
            $param = input('post.');

            if(Db::name('employee')->where('id','in',$param['ids'])->update(['gid'=>$param['gid'],'update_time'=>time()])){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $uid = session('admin_auth.admin_id');
            $groups =  Db::name('groups')->where('uid',$uid)->field('name,id')->select();
            return $this->fetch('addgroup',['groups'=>$groups,'ids'=>input('ids')]);
        }
    }

    //分类删除
    public function employee_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if(session('admin_auth.admin_id') != 1) $this->error('无权进行此操作');

            Db::name('employee')->where('id','in',$param['id'])->delete();
            insert_admin_log('删除了保险公司');
            $this->success('删除成功');
        }
    }

    //导出
    //导出留言
    public function expEmployee(){

        $start_time = input('start_time','1970-10-01');
        $end_time = input('end_time',date('Y-m-d',time()));
        $ccode = input('ccode');
        $code = input('code');
        $aid = input('aid');


        if(input('start_time') != ''){
            $start_time = strtotime($start_time);
        }else{
            $start_time = strtotime('1970-10-01');
        }

        if(input('end_time') != ''){
            $end_time = strtotime($end_time);
        }else{
            $end_time = time();
        }
        $map = [['a.create_time','between',[$start_time,$end_time]]];

        if(!empty($aid)){
            array_push($map,['a.aid',$aid]);
        }

        if(!empty( $ccode)){
            array_push($map,['b.code','like','%'.$ccode.'%']);
        }

        if(!empty($code)){
            array_push($map,['a.code','like','%'.$code.'%']);
        }


        $data = Db::name('employee')
            ->alias('a')
            ->join('channel b','a.channelid = b.id','left')
            ->join('groups c','a.gid = c.id','left')
            ->where($map)
            ->whereBetweenTime('a.create_time', $start_time, $end_time)
            ->order('a.create_time')
            ->field('a.*,b.name as cname,b.code as ccode,c.name as groupsname')->select()->toArray();

        foreach($data as $k => $v){
            $data[$k]['create_time'] =  date('Y-m-d H:i:s',$v['create_time']);
        }

        // 引入类库
         $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();;

        // 设置当前sheet
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->setTitle('业务员信息');
        $list = ['渠道码','渠道名称','员工码','所在分组','微信昵称','手机号','初次登陆日期'];
        $strarr = ['A','B','C','D','E','F','G','H','I','J','K', 'L','M','N',"O",'P','Q','R','S','T','U','V',
            'W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP',
            'AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ',
            'BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ','CA','CB','CC','CD','CE','CF','CG'];

        foreach($list as $k => $v){
            $obj->getActiveSheet()->setCellValue($strarr[$k]. 1,$v);
        }

        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('F')->setWidth(40);
        $obj->getActiveSheet()->getColumnDimension('G')->setWidth(40);
        $count = 2;
        foreach($data as $k => $v){
            $obj->getActiveSheet()->setCellValue('A'.$count,$v['ccode']);
            $obj->getActiveSheet()->setCellValue('B'.$count,$v['cname']);
            $obj->getActiveSheet()->setCellValue('C'.$count,$v['code']);
            $obj->getActiveSheet()->setCellValue('D'.$count,$v['groupsname']);
            $obj->getActiveSheet()->setCellValue('E'.$count,$v['name']);
            $obj->getActiveSheet()->setCellValue('F'.$count,$v['phone']);
            $obj->getActiveSheet()->setCellValue('G'.$count,$v['create_time']);
            $obj->getActiveSheet()->getRowDimension($count)->setRowHeight(40);
            $count++;
        }
        $obj->getActiveSheet()->getStyle('A1:Q2000')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
        $obj->getActiveSheet()->getStyle('A1:Q2000')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);

        $fileName =  '业务员信息';
        // 导出
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8');
        header('Content-Disposition: attachment;filename="' . $fileName.'.xlsx');
        header('Cache-Control: max-age=1');
        $objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($obj, 'Xlsx');
        $objWriter->save('php://output');
        exit;
    }
}